package io.renren.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.dao.ApDao;
import io.renren.entity.ApEntity;
import io.renren.param.ApHeartBeatData;
import io.renren.param.ApHeartBeatParam;
import io.renren.service.ApService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("apService")
public class ApServiceImpl extends ServiceImpl<ApDao, ApEntity> implements ApService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String apId = (String) params.get("apId");
        String user = (String) params.get("user");
        List<String> users = (List<String>) params.get("users");
        String mac = (String) params.get("mac");
        String status = (String) params.get("status");
        String ip = (String) params.get("ip");

        IPage<ApEntity> page = this.page(
                new Query<ApEntity>().getPage(params),
                new QueryWrapper<ApEntity>()
                        .eq(apId != null, "ap_id", apId)
                        .eq(user != null, "user", user)
                        .in(users != null && users.size() > 0, "user", users)
                        .eq(mac != null, "mac", mac)
                        .eq(status != null, "status", status)
                        .eq(ip != null, "ip", ip)
        );

        return new PageUtils(page);
    }

    public boolean batchSave(ApHeartBeatParam param) {

        List<ApEntity> apEntities = new ArrayList<>();
        for (ApHeartBeatData datum : param.getData()) {
            String mac = datum.getMac();
            Map<String, Object> map = new HashMap<>();
            map.put("mac", mac);

            PageUtils aps = queryPage(map);
            if (CollectionUtil.isNotEmpty(aps.getList())) {
                //ap 已存在，更新
                for (ApEntity apEntity : (List<ApEntity>) aps.getList()) {
                    Long id = apEntity.getId();
                    apEntity = datum.convertFor(datum);
                    apEntity.setId(id);
                    /*esl.setUser(datum.getUser());
                    esl.setType(param.getType());
                    esl.setStatusNo(param.getStatusNo());
                    esl.setBattery(datum.getBattery());
                    esl.setApId(datum.getApId());
                    esl.setEslId(datum.getEslId());
                    esl.setFirmwareId(datum.getFirmwareId());
                    esl.setLastHbTime(datum.getLastBbTime());
                    esl.setNfc(datum.getNfc());
                    esl.setReserve(datum.getReserve());
                    esl.setRfPower(datum.getRfPower());
                    esl.setRomVersion(datum.getRomVersion());
                    esl.setSetWor(datum.getSetWor());*/
                    updateById(apEntity);
                }
            } else {
                //不存在，新增
                ApEntity apEntity = datum.convertFor(datum);
                apEntity.setStatusNo(param.getStatusNo());
                apEntity.setType(param.getType());
                apEntities.add(apEntity);
            }
        }
        if (CollectionUtil.isNotEmpty(apEntities)) {
            saveBatch(apEntities);
        }
        return true;
    }

    public ApEntity queryByApId(String apId) {
        return baseMapper.selectOne(new QueryWrapper<ApEntity>()
                .eq("ap_id", apId));
    }

    public ApEntity queryByApMac(String apMac) {
        return baseMapper.selectOne(new QueryWrapper<ApEntity>()
                .eq("mac", apMac));
    }

    public List<ApEntity> queryByUser(String user) {
        return baseMapper.selectList(new QueryWrapper<ApEntity>()
                .eq("user", user));
    }
}
